package com.lepu.app.fun.my.adapter;

import java.util.ArrayList;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.app.utils.LoginUtil;
import com.app.utils.Setting;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.utils.main.UtilityBase;
import com.core.lib.utils.photo.UniversalImageLoadTool;
import com.core.lib.widget.CircularImageView;
import com.lepu.app.fun.bbs.BbsDetailActivity;
import com.lepu.app.fun.health.activity.HealthWebActivity;
import com.lepu.app.fun.index.bean.BeanIndexNew;
import com.lepu.app.fun.my.MyLectureDetailActivity;
import com.lepu.app.fun.my.MyMassegeDetailActivity;
import com.lepu.app.fun.my.bean.BeanMsgSystem;
import com.lepu.app.main.activity.AppBroswerActivity;
import com.lepu.pasm.R;

public class AdapterMyMassege extends BaseAdapter {

	private LayoutInflater mLayoutInflater = null;
	private MyMassegeDetailActivity mContext = null;
	private ArrayList<BeanMsgSystem> mArrayList = null;
	private int mType = 0;

	public AdapterMyMassege(MyMassegeDetailActivity context, ArrayList<BeanMsgSystem> arrayList, int type) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mArrayList = arrayList;
		mType = type;
	}

	@Override
	public int getCount() {
		int size = 0;
		if (mArrayList != null && mArrayList.size() > 0) {
			size = mArrayList.size();
		}
		return size;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		ViewHolder holder = null;

		if (convertView == null) {
			if(mType == 1 || mType == 3 || mType == 4){  //系统消息
				convertView = mLayoutInflater.inflate(R.layout.my_massege_os_item, null);

				holder = new ViewHolder();
	            
				LinearLayout itemLayout = (LinearLayout) convertView.findViewById(R.id.itemLayout);
				TextView contentTextView = (TextView) convertView.findViewById(R.id.contentTextView);
				TextView timeTextView = (TextView) convertView.findViewById(R.id.timeTextView);

				holder.itemLayout = itemLayout;
				holder.contentTextView = contentTextView;
				holder.timeTextView = timeTextView;
			}else if(mType == 2){   //粉丝
				convertView = mLayoutInflater.inflate(R.layout.my_massege_fans_item, null);

				holder = new ViewHolder();
	            
				LinearLayout itemLayout = (LinearLayout) convertView.findViewById(R.id.itemLayout);
				CircularImageView iconImage = (CircularImageView) convertView.findViewById(R.id.iconImageView);
				TextView contentTextView = (TextView) convertView.findViewById(R.id.contentTextView);
				TextView timeTextView = (TextView) convertView.findViewById(R.id.timeTextView);
				ImageView newImage = (ImageView) convertView.findViewById(R.id.newImageView);
				
				holder.itemLayout = itemLayout;
				holder.newImage = newImage;
				holder.iconImage = iconImage;
				holder.contentTextView = contentTextView;
				holder.timeTextView = timeTextView;
			}

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final BeanMsgSystem item = mArrayList.get(position);
		if(mType == 1 || mType == 3 || mType == 4){ //系统消息
			holder.contentTextView.setText(item.content);
			holder.timeTextView.setText(item.time);
			
			if(BeanIndexNew.isExist(item.messageId)){
				holder.contentTextView.setTextColor(mContext.getResources().getColor(R.color.color_text_black2));
			}else{
				holder.contentTextView.setTextColor(mContext.getResources().getColor(R.color.color_text_black));
			}
			
			if(mType == 1){
				holder.itemLayout.setOnClickListener(new ViewClick(item, holder.contentTextView));
			}
			
		}else if(mType == 2){ //粉丝
			// 头像
			String imageUrl = Setting.getHeadIconUrl(item.targetId);
			UniversalImageLoadTool.disPlay(imageUrl, holder.iconImage,R.drawable.app_ic_default_avatar);
			
			//new 
			if(BeanIndexNew.isExist(item.messageId)){
				holder.newImage.setVisibility(View.INVISIBLE);
			}else{
				holder.newImage.setVisibility(View.VISIBLE);
			}
			holder.contentTextView.setText(item.content);
			holder.timeTextView.setText(item.time);
			
			holder.itemLayout.setOnClickListener(new ViewClick(item, holder.newImage));
		}
		
		return convertView;
	}

	class ViewClick implements View.OnClickListener{
		public BeanMsgSystem bean;
        public ImageView imageView;
        public TextView textView;
		
		public ViewClick(BeanMsgSystem bean, ImageView imageView){
			this.imageView = imageView;
			this.bean = bean;
		}
		
		public ViewClick(BeanMsgSystem bean, TextView textView){
			this.textView = textView;
			this.bean = bean;
		}
		
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.itemLayout:
				if(mType == 1){
					int targetType = UtilityBase.parseInt(bean.targetType);
					
					if(targetType == 1){  //用户资料
						LoginUtil.jumpUserInfo(mContext,bean.targetId);
					}else if(targetType == 2){  //帖子详情
						Intent intent = new Intent(mContext,BbsDetailActivity.class);
						intent.putExtra("threadId", bean.targetId);
						mContext.startActivity(intent, true);
					}else if(targetType == 3){  //讲座 活动
						Intent intentInfo = new Intent(mContext,MyLectureDetailActivity.class);
						intentInfo.putExtra("LectureID", bean.targetId);
						mContext.startActivity(intentInfo, true);
					}else if(targetType == 4){  //优惠券
						if(LoginUtil.jumpLogin(mContext)){
							return;
						}
						
						Intent youHuiQuanIntent = new Intent(mContext,AppBroswerActivity.class);
						youHuiQuanIntent.putExtra("url",Setting.getMyYouHuiQuanUrl(bean.userId));
						youHuiQuanIntent.putExtra("title", "我的优惠券");
						mContext.startActivity(youHuiQuanIntent, true);

					}else if(targetType == 5){  //陪诊

						Intent intent = new Intent(mContext, HealthWebActivity.class);
						intent.putExtra("url", Setting.getSuiXinZhenMainUrl());
						intent.putExtra("title", mContext.getString(R.string.health_title_pei_zhen));
						intent.putExtra("type", HealthWebActivity.TYPE_PEI_ZHEN);
						mContext.startActivity(intent, true);
					}else if(targetType == 6){  //转诊

						Intent intentZhuanZhen = new Intent(mContext, HealthWebActivity.class);
						intentZhuanZhen.putExtra("url", Setting.getZhuanZhenMainUrl());
						intentZhuanZhen.putExtra("title", mContext.getString(R.string.health_title_zhuan_zhen));
						intentZhuanZhen.putExtra("type", HealthWebActivity.TYPE_ZHUAN_ZHEN);
						mContext.startActivity(intentZhuanZhen, true);
					}else if(targetType == 100){  //跳转其它url

						if (TextUtils.isEmpty(bean.targetUrl)) {
							return;
						}
						Intent urlIntent = new Intent(mContext,
								AppBroswerActivity.class);
						urlIntent.putExtra("url", bean.targetUrl);
						urlIntent.putExtra("title", "");
						mContext.startActivity(urlIntent, true);
					}
					
					BeanIndexNew.insertTargetId(bean.messageId);
					textView.setTextColor(mContext.getResources().getColor(R.color.color_text_black2));
					
				}else if(mType == 2){ //粉丝
					LoginUtil.jumpUserInfo((BaseFragmentActivity) mContext,bean.targetId);
					BeanIndexNew.insertTargetId(bean.messageId);
					imageView.setVisibility(View.INVISIBLE);
				}else if(mType == 3){
					//陪诊
				}else if(mType == 4){
					//转诊
				}
				break;

			default:
				break;
			}
		}
		
	}
	
	private static class ViewHolder {
		LinearLayout itemLayout;
		ImageView newImage;
		CircularImageView iconImage;
		TextView contentTextView;
		TextView timeTextView;
	}
}
